package com.whfc.base.redis.impl;

import com.alibaba.fastjson.JSONObject;
import com.whfc.base.dto.AlarmCacheDTO;
import com.whfc.base.redis.AlarmRecordRedisDao;
import com.whfc.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
 * @author sun_guodong
 * @description
 * @date 2019-12-06
 */
@Repository
public class AlarmRecordRedisDaoImpl implements AlarmRecordRedisDao {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_MODULE_RULE_OBJ = "alarm-module-rule-obj:{0}:{1}:{2}:{3}";
    private static final String KEY_MODULE_OBJ = "alarm-module-obj:{0}:{1}:{2}";
    private static final String KEY_MODULE_DEPT = "alarm-module-dept:{0}:{1}:{2}";
    private static final String KEY_MODULE_DEPT_RULE = "alarm-module-dept-rule:{0}:{1}:{2}:{3}";

    @Override
    public void setAlarmRecord(Integer moduleType, Integer ruleType, String objectId, AlarmCacheDTO value) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_RULE_OBJ, dateStr, moduleType, ruleType, objectId);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(value), 1, TimeUnit.DAYS);
    }

    @Override
    public AlarmCacheDTO getAlarmRecord(Integer moduleType, Integer ruleType, String objectId) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_RULE_OBJ, dateStr, moduleType, ruleType, objectId);
        return this.getValue(key);
    }

    @Override
    public void setAlarmRecord(Integer moduleType, String objectId, AlarmCacheDTO value) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_OBJ, dateStr, moduleType, objectId);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(value), 1, TimeUnit.DAYS);
    }

    @Override
    public AlarmCacheDTO getAlarmRecord(Integer moduleType, String objectId) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_OBJ, dateStr, moduleType, objectId);
        return this.getValue(key);
    }

    @Override
    public void setAlarmRecord(Integer moduleType, Integer deptId, AlarmCacheDTO value) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_DEPT, dateStr, moduleType, deptId);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(value), 1, TimeUnit.DAYS);
    }

    @Override
    public AlarmCacheDTO getAlarmRecord(Integer moduleType, Integer deptId) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_DEPT, dateStr, moduleType, deptId);
        return this.getValue(key);
    }

    @Override
    public void setAlarmRecord(Integer moduleType, Integer deptId, Integer ruleType, AlarmCacheDTO value) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_DEPT_RULE, dateStr, moduleType, deptId, ruleType);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(value), 1, TimeUnit.DAYS);
    }

    @Override
    public AlarmCacheDTO getAlarmRecord(Integer moduleType, Integer deptId, Integer ruleType) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_DEPT_RULE, dateStr, moduleType, deptId, ruleType);
        return this.getValue(key);
    }

    @Override
    public void delAlarmRecord(Integer moduleType, Integer ruleType, String objectId) {
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        String key = MessageFormat.format(KEY_MODULE_RULE_OBJ, dateStr, moduleType, ruleType, objectId);
        redisTemplate.delete(key);
    }


    /**
     * 获取值
     *
     * @param key
     * @return
     */
    private AlarmCacheDTO getValue(String key) {
        AlarmCacheDTO data = new AlarmCacheDTO();
        String s = redisTemplate.opsForValue().get(key);
        if (!StringUtils.isBlank(s)) {
            data = JSONObject.parseObject(s, AlarmCacheDTO.class);
        }
        return data;
    }
}

